#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_PRODUCTS 10000
#define MAX_NAME_LENGTH 10

typedef struct {
    char name[MAX_NAME_LENGTH];
    double unitPrice;
    int soldQuantity;
    double salesAmount;
    int inventoryQuantity;
    double inventoryAmount;
    int returnQuantity;
    double returnAmount;
    double weight;
    double volume;
} Product;

// 插入排序函数（与多线程版本保持一致）
void insertionSort(Product *arr, int start, int end) {
    int i, j;
    Product key;
    for (i = start + 1; i <= end; i++) {
        key = arr[i];
        j = i - 1;
        while (j >= start && arr[j].unitPrice > key.unitPrice) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}

// 从文件读取数据到结构数组
int readDataFromFile(const char *filename, Product products[]) {
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        perror("无法打开文件");
        return -1;
    }

    int count = 0;
    // 跳过标题行
    char line[256];
    fgets(line, sizeof(line), file);

    while (fscanf(file, "%9s %lf %d %lf %d %lf %d %lf %lf %lf",
                 products[count].name, &products[count].unitPrice, &products[count].soldQuantity,
                 &products[count].salesAmount, &products[count].inventoryQuantity,
                 &products[count].inventoryAmount, &products[count].returnQuantity,
                 &products[count].returnAmount, &products[count].weight, &products[count].volume) != EOF &&
           count < MAX_PRODUCTS) {
        count++;
    }

    fclose(file);
    return count;
}

// 输出结构数组内容
void printProducts(Product products[], int count) {
    printf("%-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n",
           "商品名", "单价", "销出数量", "销售额", "库存数量", "库存金额", "退货量", "退货金额", "重量", "体积");
    for (int i = 0; i < count; i++) {
        printf("%-10s %-10.2f %-10d %-10.2f %-10d %-10.2f %-10d %-10.2f %-10.2f %-10.2f\n",
               products[i].name, products[i].unitPrice, products[i].soldQuantity,
               products[i].salesAmount, products[i].inventoryQuantity, products[i].inventoryAmount,
               products[i].returnQuantity, products[i].returnAmount, products[i].weight, products[i].volume);
    }
}

int main() {
    Product products[MAX_PRODUCTS];
    int productCount = readDataFromFile("商品销售情况.txt", products);
    if (productCount == -1) {
        return 1;
    }

    clock_t start_time, end_time;
    double total_time;

    // 记录开始时间
    start_time = clock();

    // 单线程插入排序（处理整个数组）
    insertionSort(products, 0, 9999);

    // 记录结束时间并计算总时间
    end_time = clock();
    total_time = ((double) (end_time - start_time)) / CLOCKS_PER_SEC;

    // 输出排序后的结果
    printProducts(products, productCount);
    
    // 输出排序总时间
    printf("\n单线程排序总时间: %.6f 秒\n", total_time);

    return 0;
}


//#include <stdio.h>
//#include <stdlib.h>
//#include <pthread.h>
//#include <string.h>
//#include <time.h>
//
//#define MAX_PRODUCTS 10000
//#define MAX_NAME_LENGTH 10
//
//typedef struct {
//    char name[MAX_NAME_LENGTH];
//    double unitPrice;
//    int soldQuantity;
//    double salesAmount;
//    int inventoryQuantity;
//    double inventoryAmount;
//    int returnQuantity;
//    double returnAmount;
//    double weight;
//    double volume;
//} Product;
//
//// 插入排序函数
//void insertionSort(Product *arr, int start, int end) {
//    int i, j;
//    Product key;
//    for (i = start + 1; i <= end; i++) {
//        key = arr[i];
//        j = i - 1;
//        while (j >= start && arr[j].unitPrice > key.unitPrice) {
//            arr[j + 1] = arr[j];
//            j = j - 1;
//        }
//        arr[j + 1] = key;
//    }
//}
//
//// 线程函数1，对数组元素0 - 4999进行排序
//void* sortFirstHalf(void* arg) {
//    Product *arr = (Product*)arg;
//    insertionSort(arr, 0, 4999);
//    pthread_exit(NULL);
//}
//
//// 线程函数2，对数组元素5000 - 9999进行排序
//void* sortSecondHalf(void* arg) {
//    Product *arr = (Product*)arg;
//    insertionSort(arr, 5000, 9999);
//    pthread_exit(NULL);
//}
//
//// 2路归并排序函数
//void merge(Product *arr, int left, int mid, int right) {
//    int i, j, k;
//    int n1 = mid - left + 1;
//    int n2 = right - mid;
//
//    // 创建临时数组
//    Product L[n1], R[n2];
//
//    // 复制数据到临时数组L[]和R[]
//    for (i = 0; i < n1; i++)
//        L[i] = arr[left + i];
//    for (j = 0; j < n2; j++)
//        R[j] = arr[mid + 1 + j];
//
//    // 归并临时数组到arr[left..right]
//    i = 0; // 初始化第一个子数组的索引
//    j = 0; // 初始化第二个子数组的索引
//    k = left; // 初始归并子数组的索引
//    while (i < n1 && j < n2) {
//        if (L[i].unitPrice <= R[j].unitPrice) {
//            arr[k] = L[i];
//            i++;
//        } else {
//            arr[k] = R[j];
//            j++;
//        }
//        k++;
//    }
//
//    // 复制L[]的剩余元素
//    while (i < n1) {
//        arr[k] = L[i];
//        i++;
//        k++;
//    }
//
//    // 复制R[]的剩余元素
//    while (j < n2) {
//        arr[k] = R[j];
//        j++;
//        k++;
//    }
//}
//
//// 从文件读取数据到结构数组
//int readDataFromFile(const char *filename, Product products[]) {
//    FILE *file = fopen(filename, "r");
//    if (file == NULL) {
//        perror("无法打开文件");
//        return -1;
//    }
//
//    int count = 0;
//    // 跳过标题行
//    char line[256];
//    fgets(line, sizeof(line), file);
//
//    while (fscanf(file, "%9s %lf %d %lf %d %lf %d %lf %lf %lf",
//                 products[count].name, &products[count].unitPrice, &products[count].soldQuantity,
//                 &products[count].salesAmount, &products[count].inventoryQuantity,
//                 &products[count].inventoryAmount, &products[count].returnQuantity,
//                 &products[count].returnAmount, &products[count].weight, &products[count].volume) != EOF &&
//           count < MAX_PRODUCTS) {
//        count++;
//    }
//
//    fclose(file);
//    return count;
//}
//
//// 输出结构数组内容
//void printProducts(Product products[], int count) {
//    printf("%-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n",
//           "商品名", "单价", "销出数量", "销售额", "库存数量", "库存金额", "退货量", "退货金额", "重量", "体积");
//    for (int i = 0; i < count; i++) {
//        printf("%-10s %-10.2f %-10d %-10.2f %-10d %-10.2f %-10d %-10.2f %-10.2f %-10.2f\n",
//               products[i].name, products[i].unitPrice, products[i].soldQuantity,
//               products[i].salesAmount, products[i].inventoryQuantity, products[i].inventoryAmount,
//               products[i].returnQuantity, products[i].returnAmount, products[i].weight, products[i].volume);
//    }
//}
//
//int main() {
//    Product products[MAX_PRODUCTS];
//    int productCount = readDataFromFile("商品销售情况.txt", products);
//    if (productCount == -1) {
//        return 1;
//    }
//
//    pthread_t thread1, thread2;
//    clock_t start_time, end_time;
//    double total_time;
//
//    // 记录开始时间
//    start_time = clock();
//
//    // 创建线程1
//    if (pthread_create(&thread1, NULL, sortFirstHalf, products) != 0) {
//        perror("线程1创建失败");
//        return 1;
//    }
//    // 创建线程2
//    if (pthread_create(&thread2, NULL, sortSecondHalf, products) != 0) {
//        perror("线程2创建失败");
//        pthread_cancel(thread1);
//        return 1;
//    }
//
//    // 等待线程1和线程2完成
//    if (pthread_join(thread1, NULL) != 0) {
//        perror("线程1连接失败");
//        return 1;
//    }
//    if (pthread_join(thread2, NULL) != 0) {
//        perror("线程2连接失败");
//        return 1;
//    }
//
//    // 主线程进行2路归并排序
//    merge(products, 0, 4999, 9999);
//
//    // 记录结束时间并计算总时间
//    end_time = clock();
//    total_time = ((double) (end_time - start_time)) / CLOCKS_PER_SEC;
//
//    // 输出排序后的结果
//    printProducts(products, productCount);
//    
//    // 输出排序总时间
//    printf("\n排序总时间: %.6f 秒\n", total_time);
//
//    return 0;
//}
//
//

//#include <stdio.h>
//#include <stdlib.h>
//#include <pthread.h>
//#include <string.h>
//
//#define MAX_PRODUCTS 10000
//#define MAX_NAME_LENGTH 10
//
//// 定义商品结构体
//typedef struct {
//    char name[MAX_NAME_LENGTH];
//    double unitPrice;
//    int soldQuantity;
//    double salesAmount;
//    int inventoryQuantity;
//    double inventoryAmount;
//    int returnQuantity;
//    double returnAmount;
//    double weight;
//    double volume;
//} Product;
//
//// 插入排序函数
//void insertionSort(Product *arr, int start, int end) {
//    int i, j;
//    Product key;
//    for (i = start + 1; i <= end; i++) {
//        key = arr[i];
//        j = i - 1;
//        while (j >= start && arr[j].unitPrice > key.unitPrice) {
//            arr[j + 1] = arr[j];
//            j = j - 1;
//        }
//        arr[j + 1] = key;
//    }
//}
//
//// 线程函数1，对数组元素0 - 4999进行排序
//void* sortFirstHalf(void* arg) {
//    Product *arr = (Product*)arg;
//    insertionSort(arr, 0, 4999);
//    pthread_exit(NULL);
//}
//
//// 线程函数2，对数组元素5000 - 9999进行排序
//void* sortSecondHalf(void* arg) {
//    Product *arr = (Product*)arg;
//    insertionSort(arr, 5000, 9999);
//    pthread_exit(NULL);
//}
//
//// 从文件读取数据到结构数组
//int readDataFromFile(const char *filename, Product products[]) {
//    FILE *file = fopen(filename, "r");
//    if (file == NULL) {
//        perror("无法打开文件");
//        return -1;
//    }
//
//    int count = 0;
//    // 跳过标题行
//    char line[256];
//    fgets(line, sizeof(line), file);
//
//    while (fscanf(file, "%9s %lf %d %lf %d %lf %d %lf %lf %lf",
//                 products[count].name, &products[count].unitPrice, &products[count].soldQuantity,
//                 &products[count].salesAmount, &products[count].inventoryQuantity,
//                 &products[count].inventoryAmount, &products[count].returnQuantity,
//                 &products[count].returnAmount, &products[count].weight, &products[count].volume) != EOF &&
//           count < MAX_PRODUCTS) {
//        count++;
//    }
//
//    fclose(file);
//    return count;
//}
//
//// 输出结构数组内容
//void printProducts(Product products[], int count) {
//    printf("%-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n",
//           "商品名", "单价", "销出数量", "销售额", "库存数量", "库存金额", "退货量", "退货金额", "重量", "体积");
//    for (int i = 0; i < count; i++) {
//        printf("%-10s %-10.2f %-10d %-10.2f %-10d %-10.2f %-10d %-10.2f %-10.2f %-10.2f\n",
//               products[i].name, products[i].unitPrice, products[i].soldQuantity,
//               products[i].salesAmount, products[i].inventoryQuantity, products[i].inventoryAmount,
//               products[i].returnQuantity, products[i].returnAmount, products[i].weight, products[i].volume);
//    }
//}
//
//int main() {
//    Product products[MAX_PRODUCTS];
//    int productCount = readDataFromFile("商品销售情况.txt", products);
//    if (productCount == -1) {
//        return 1;
//    }
//
//    pthread_t thread1, thread2;
//    // 创建线程1
//    if (pthread_create(&thread1, NULL, sortFirstHalf, products) != 0) {
//        perror("线程1创建失败");
//        return 1;
//    }
//    // 创建线程2
//    if (pthread_create(&thread2, NULL, sortSecondHalf, products) != 0) {
//        perror("线程2创建失败");
//        pthread_cancel(thread1);
//        return 1;
//    }
//
//    // 等待线程1和线程2完成
//    if (pthread_join(thread1, NULL) != 0) {
//        perror("线程1连接失败");
//        return 1;
//    }
//    if (pthread_join(thread2, NULL) != 0) {
//        perror("线程2连接失败");
//        return 1;
//    }
//
//    // 输出排序后的结果
//    printProducts(products, productCount);
//
//    return 0;
//}
//


//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//
//#define MAX_PRODUCTS 10000
//#define MAX_NAME_LENGTH 10
//
//// 定义商品结构体
//typedef struct {
//    char name[MAX_NAME_LENGTH];
//    double unitPrice;
//    int soldQuantity;
//    double salesAmount;
//    int inventoryQuantity;
//    double inventoryAmount;
//    int returnQuantity;
//    double returnAmount;
//    double weight;
//    double volume;
//} Product;
//
//// 比较函数，用于qsort排序，以单价为关键字
//int compareByUnitPrice(const void *a, const void *b) {
//    Product *productA = (Product *)a;
//    Product *productB = (Product *)b;
//    if (productA->unitPrice < productB->unitPrice) return -1;
//    if (productA->unitPrice > productB->unitPrice) return 1;
//    return 0;
//}
//
//// 从文件读取数据到结构数组
//int readDataFromFile(const char *filename, Product products[]) {
//    FILE *file = fopen(filename, "r");
//    if (file == NULL) {
//        perror("无法打开文件");
//        return -1;
//    }
//
//    int count = 0;
//    // 跳过标题行
//    char line[256];
//    fgets(line, sizeof(line), file);
//
//    while (fscanf(file, "%9s %lf %d %lf %d %lf %d %lf %lf %lf",
//                 products[count].name, &products[count].unitPrice, &products[count].soldQuantity,
//                 &products[count].salesAmount, &products[count].inventoryQuantity,
//                 &products[count].inventoryAmount, &products[count].returnQuantity,
//                 &products[count].returnAmount, &products[count].weight, &products[count].volume) != EOF &&
//           count < MAX_PRODUCTS) {
//        count++;
//    }
//
//    fclose(file);
//    return count;
//}
//
//// 输出结构数组内容
//void printProducts(Product products[], int count) {
//    printf("%-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n",
//           "商品名", "单价", "销出数量", "销售额", "库存数量", "库存金额", "退货量", "退货金额", "重量", "体积");
//    for (int i = 0; i < count; i++) {
//        printf("%-10s %-10.2f %-10d %-10.2f %-10d %-10.2f %-10d %-10.2f %-10.2f %-10.2f\n",
//               products[i].name, products[i].unitPrice, products[i].soldQuantity,
//               products[i].salesAmount, products[i].inventoryQuantity, products[i].inventoryAmount,
//               products[i].returnQuantity, products[i].returnAmount, products[i].weight, products[i].volume);
//    }
//}
//
//int main() {
//    Product products[MAX_PRODUCTS];
//    int productCount = readDataFromFile("商品销售情况.txt", products);
//    if (productCount == -1) {
//        return 1;
//    }
//
//    // 以单价为关键字排序
//    qsort(products, productCount, sizeof(Product), compareByUnitPrice);
//
//    // 输出排序后的结果
//    printProducts(products, productCount);
//
//    return 0;
//}
